<?php
namespace support\middleware;

use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;

/**
 * 跨域
 * Class AccessControl
 * @package support\middleware
 */
class AccessControl implements MiddlewareInterface
{
    public function process(Request $request, callable $next) : Response
    {
        /** @var Response $response */
        if($request->method() == 'OPTIONS'){
            $response = response('');
            $response->withHeaders([
                'Access-Control-Allow-Origin' => 'http://localhost:8082',
                'Access-Control-Allow-Methods' => 'GET,POST,PUT,DELETE,OPTIONS',
                'Access-Control-Allow-Credentials' => 'true',
                'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With, Accept, Origin, Authorization, Headers, token , Authori-zation',
            ]);
        }else{
            $response = $next($request);
        }

        return $response;
    }
}